package org.lmind.osgi.bundle.log4j.internal;

import java.io.File;
import java.net.URL;

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

	@Override
	public void start(BundleContext context) throws Exception {
		
		if (!configPropertiesFile()) {
			configXmlFile();
		}
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		
	}
	
	private boolean configPropertiesFile() {
		File log4j = new File("log4j.properties");
		if (log4j.exists() && log4j.isFile()) {
			PropertyConfigurator.configure(log4j.toString());
			return true;
		}
		return false;
	}
	
	private void configXmlFile() {
		File log4jXml = new File("log4j.xml");
		if (log4jXml.exists() && log4jXml.isFile()) {
			DOMConfigurator.configure(log4jXml.toString());
		} else {
			URL url = this.getClass().getResource("/log4j-default.xml");
			DOMConfigurator.configure(url);
		}
	}

}
